<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;

class GoodsController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 $this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}

	//列表
	public function lst(){
		$g=M('goods');

		if(IS_POST){
			
		}else{

			$res = $g->select();
			foreach ($res as &$k) {
				$k['details'] = html_entity_decode($k['details']);  //解码html
			}
			
		}

		$this->assign('res',$res);
		$this->display();
	}


	//查看商品详情 / 编辑页面

	public function goods_edit(){
		
		$g = M('goods');

		if(IS_POST){
			
			$data = I('post.','','htmlspecialchars');

			if(isset($_FILES['goods_img']['tmp_name']) && !empty($_FILES['goods_img']['tmp_name'])){
				$upload = new \Think\Upload();

				//$upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'jpeg','bmp','png');// 设置附件上传类型
			    $upload->rootPath  =     ROOT_PATH; // 设置附件上传根目录
			    $upload->savePath  =     GOODS_PATH.'/'; // 设置附件上传（子）目录
			    $upload->subName   =     array('date','Ymd');
			    $info   =   $upload->upload();
			    
			    /*if(!$info) {// 上传错误提示错误信息
	       		 	$this->error($upload->getError());

	   	    	}*/

	   	    	$data['goods_img'] = $info['goods_img']['savepath'].$info['goods_img']['savename'];
	   	    	//echo $data['goods_img'];die;
	   	    	//上传成功则 录入其它信息
   	    	}

   	    	//dump($data);die;
   	   		$res = $g->save($data);
   	   		
   	    	
   	    	if($res!==false){
   	    		$this->success('修改成功',U('lst'),1);
   	    	}else{
   	    		$this->error('修改失败','',2);
   	    	}
		}

		$id = I('get.id');
		$res = $g->find($id);
		$res['details'] = html_entity_decode($res['details']);
		$this->assign('res',$res);
		$this->display();
	}


	

	//新增商品信息
	public function add(){
		$g=M('goods');

		if(IS_POST){
			// dump(I('post.'));
			// dump($_FILES);die;
			$data = I('post.','','htmlspecialchars');
			if(isset($_FILES['goods_img']['tmp_name']) && !empty($_FILES['goods_img']['tmp_name'])){
				$upload = new \Think\Upload();

				//$upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'jpeg','bmp','png');// 设置附件上传类型
			    $upload->rootPath  =     ROOT_PATH; // 设置附件上传根目录
			    $upload->savePath  =     GOODS_PATH.'/'; // 设置附件上传（子）目录
			    $upload->subName   =     array('date','Ymd');
			    $info   =   $upload->upload();
			    
			    /*if(!$info) {// 上传错误提示错误信息
	       		 	$this->error($upload->getError());

	   	    	}*/

	   	    	$data['goods_img'] = $info['goods_img']['savepath'].$info['goods_img']['savename'];
	   	    	//echo $data['goods_img'];die;
	   	    	//上传成功则 录入其它信息
   	    	}


   	   		$res = $g->add($data);
   	   		
   	    	
   	    	if($res){
   	    		$this->success('添加成功',U('lst'),1);
   	    	}else{
   	    		$this->error('添加失败','',2);
   	    	}


		
		}
		$this->display();

	}

	//删除商品
	public function del($id){
		M('goods')->delete($id);
		$this->success('删除商品成功',U('lst'));
	}

}